﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.Domain
{
    internal static class DataTypeParserRepository
    {
        private static Dictionary<Type, IDataTypeParser> _parsers;

        static DataTypeParserRepository()
        {
            _parsers = new Dictionary<Type, IDataTypeParser>();
            _parsers.Add(typeof(int), new IntDataTypeParser());
            _parsers.Add(typeof(DateTime), new DateTimeTypeParser());
            _parsers.Add(typeof(double), new DoubleDataTypeParser());
            _parsers.Add(typeof(string), new StringDataTypeParser());
        }

        public static IDataTypeParser GetParserForType(Type t)
        {
            if (t == null)
                throw new ArgumentNullException("t");

            if (_parsers.ContainsKey(t))
                return _parsers[t];
            else
                throw new ArgumentException(String.Format("Cannot find a parser for type {0}", t.Name));
        }

        public static IDataTypeParser GetParserForType(string typeName)
        {
            if (typeName == null)
                throw new ArgumentException("typeName");

            Type t = Type.GetType(typeName);

            if (t == null)
                throw new ArgumentException(String.Format("Invalid type name: {0}", typeName));

            return GetParserForType(t);
        }
    }
}
